package alg2_bagQueueStack;

import java.util.Iterator;
//用数组来实现栈结构
public class Test1_StackWithArray<Item> implements Iterable<Item>{

    //栈元素
    private Item[] a = (Item[]) new Object[1];

    //元素数量
    private int N = 0;

    public boolean isEmpty() {
        return N == 0;
    }

    //将栈移动到一个大小为max的新数组
    public void resize(int max) {
        Item[] temp = (Item[]) new Object[max];
        for (int i = 0; i < N; i++) {
            temp[i] = a[i];
        }
        a = temp;
    }

    //将元素添加到栈顶
    public void push(Item item){
        if (N == a.length){
            resize(2*a.length);
        }
        a[N++] = item;
    }

    //从栈顶删除元素
    public Item pop(){
        Item item = a[--N];
        a[N] = null;//避免对象游离
        if (N > 0 && N == a.length/4){
            resize(a.length);
        }
        return item;
    }

    @Override
    public Iterator<Item> iterator() {
        return null;
    }
    private class ReverseArrayIterator implements Iterator<Item>{

        private int i = N;

        @Override
        public boolean hasNext() {
            return i > 0;
        }

        @Override
        public Item next() {
            return a[--i];
        }

        @Override
        public void remove() {

        }
    }

}
